﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using BDPAutodetect.Device;
using CommunityToolkit.Mvvm.ComponentModel;

namespace BDPAutodetect.Models
{
    /// <summary>
    /// 工位检测统计传输数据
    /// </summary>
    public partial class MStationDetectTotal : ObservableModel
    {
        /// <summary>
        /// 标识/编号
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 工位实例
        /// </summary>
        [ObservableProperty]
        protected MStation station;

        /// <summary>
        /// 累计检测次数
        /// </summary>
        [ObservableProperty]
        protected int detectCount;

        /// <summary>
        /// 累计检测时长(单位/s)
        /// </summary>
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(DetectDurationDisplay))]
        protected int detectDuration;

        /// <summary>
        /// 累计检测时长显示
        /// </summary>
        public string DetectDurationDisplay => ConvertDateTimeDiffDisplay(this.DetectDuration);

        /// <summary>
        /// 深度克隆
        /// </summary>
        public MStationDetectTotal CloneDeep()
        {
            return new MStationDetectTotal()
            {
                ID = this.ID,
                Station = this.Station,
                DetectDuration = this.DetectDuration,
                DetectCount = this.DetectCount
            };
        }

        /// <summary>
        /// 克隆表数据
        /// </summary>
        public TDetectRecordTotal CloneTData()
        {
            return new TDetectRecordTotal()
            {
                ID = this.ID,
                StationID = this.Station.ID,
                DetectCount = this.DetectCount,
                DetectDuration = this.DetectDuration
            };
        }

        /// <summary>
        /// 计算检测执行时间(显示字符串)
        /// </summary>
        /// <param name="argTotalSeconds">总秒数</param>
        /// <returns>返回(xx天xx时xx分xx秒)单位字符串</returns>
        protected string ConvertDateTimeDiffDisplay(int argTotalSeconds)
        {
            if (argTotalSeconds <= 0) return "0天00时00分00秒";
            try
            {
                TimeSpan ts = TimeSpan.FromSeconds(argTotalSeconds);
                return $"{ts.Days}天{ts.Hours}时{ts.Minutes}分{ts.Seconds}秒";
            }
            catch (Exception)
            {
                return "0天00时00分00秒";
            }
        }
    }
}
